<?php

namespace Web\MainSiteBundle\Controller;

use Model\ExamBundle\Model\ExamModel;
use Model\LogExamBundle\Model\LogExamModel;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\HttpFoundation\Request;

/**
 * @route("LogExam")
 */
class LogExamController extends WebBaseController
{
    /**
     * @route("/Prepare",name="WebLogExamPrepare")
     */
    public function WebExamAdd(Request $request)
    {
        $examId = 1;
        $logExamModel = new ExamModel($this->getDoctrine());
        $data = $logExamModel->getTotalData4Exam($examId,0);
        $data = json_encode($data);
//        return $this->json_out(1,"全部数据",$data);
        return $this->render("WebMainSiteBundle:Exam:/Show/layout.html.twig",array(
            "data"  => $data,
        ));
    }

    /**
     * @route("/CollectUserData",name="WebExamCollectUserExamData")
     */
    public function WebExamCollectUserExamData(Request $request)
    {
        $user = $this->needToBeUser();
        $userId = $user->getId();
        $logExamModel = new LogExamModel($this->getDoctrine());
        if ($request->getMethod() == "POST") {

            if (!$action = $this->getParamRequired("action")) {
                return $this->json_out(0,"数据指向未找到");
            }

            //以下是数据统计的相关功能
            switch ($action) {
                case "startExam":
                    //新的统计数据插入或更新到数据库
                    //* 开始考试：新增一条记录，插入当前时间戳
                    if (!$examId = $this->getParamRequired("examId")) {
                        return $this->json_out(0,"试卷未找到");
                    }
                    $logExam = $logExamModel->startExam( $examId , $userId );
                    if ($logExam) {
                        return $this->json_out(1,"开始记录考试");
                    }
                    break;
                case "finishExam":
                    //* 结束考试(交卷):设置结束时间，处理答题数据；
                    //todo
                    break;
                case "postData":
                    //* 开始答题：记录答题开始的时间，记录答题结束的时间，记录题目提交的答案，（记录对题目做得标记，【难题|不确定|……】）
                    //题目数据模型
                    $questionData = array(
                        "questionId"    => 1,
                        "startTime"     => 0,
                        "endTIme"       => 100,
                        "isDifficult"   => 0,
                        "isDone"        => 1,
                        "choices"       => array(
                            array(
                                "choiceId"      => 1,
                                "isSelected"    => 0,//未选中=0，选中=1
                                "timeSpend"     => 10,//单位：秒
                            ),
                            "……"
                        )
                    );
                    break;
            }

        } else {
            //取出所有的统计数据
            var_dump($request->cookies);
            die;
        }
    }


}
